package poi.copypic1;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

public class ImageResizerUtil {

	// 比例
	private static int propX = 16;
	private static int propY = 9;

	/**
	 * 固定比例截取图片
	 *
	 * @param inputFile
	 * @param outFile
	 * @throws IOException
	 */
	public static String cutJPG(String inputFile) throws IOException {

		int[] w_h = getImgW_H(inputFile);
		int width = w_h[0];
		int height = w_h[1];

		// 计算截图的坐标位置
		boolean prop = width / propX > height / propY;

		int x = 0;
		int y = 0;

		// 图片过宽
		if (prop) {
			x = (width - height / propY * propX) / 2;
			width = height / propY * propX;
		} else {
			// 图片过高
			y = (height - width / propX * propY) / 2;
			height = width / propX * propY;
		}

		ImageInputStream imageStream = null;

		try {
			Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
			ImageReader reader = readers.next();
			imageStream = ImageIO.createImageInputStream(new FileInputStream(inputFile));
			reader.setInput(imageStream, true);
			ImageReadParam param = reader.getDefaultReadParam();
			System.out.println(reader.getWidth(0));
			System.out.println(reader.getHeight(0));
			Rectangle rect = new Rectangle(x, y, width, height);
			param.setSourceRegion(rect);
			BufferedImage bi = reader.read(0, param);
			String outFile = inputFile.replace(".jpg", System.currentTimeMillis() + "Tmp.jpg");
			FileOutputStream outputStream = new FileOutputStream(outFile);
			ImageIO.write(bi, "jpg", outputStream);
			outputStream.flush();
			imageStream.flush();
			return outFile;
		} catch (IOException e) {
		} finally {
			imageStream.close();
		}
		return inputFile;
	}

	/**
	 * 获取图片的真实像素
	 *
	 * @param imgPath
	 * @return
	 */
	@SuppressWarnings({ "hiding", "unused" })
	public static int[] getImgW_H(String imgPath) {
		File file = new File(imgPath);
		FileChannel fc = null;
		if (file.exists() && file.isFile()) {
			try {
				@SuppressWarnings("resource")
				FileInputStream fs = new FileInputStream(file);
				fc = fs.getChannel();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		BufferedImage bi = null;
		try {
			bi = ImageIO.read(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		int width = bi.getWidth();
		int height = bi.getHeight();
		int[] result = { width, height };
		return result;
	}

	/***
	 * 等比例改变图片像素
	 *
	 * @param srcImgPath
	 * @param distImgPath
	 * @param multiple
	 *            改变的比例
	 * @throws IOException
	 * @throws InterruptedException 
	 */
	public static void resizeImage(String srcImgPath, String distImgPath, double multiple) throws IOException, InterruptedException {
		File srcFile = new File(srcImgPath);
		Image srcImg = ImageIO.read(srcFile);
		int[] w_h = getImgW_H(srcImgPath);
		int w = (int) (w_h[0] * multiple);
		int h = (int) (w_h[1] * multiple);
		BufferedImage buffImg = null;
		buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		buffImg.getGraphics().drawImage(srcImg.getScaledInstance(w, h, Image.SCALE_DEFAULT), 0, 0, null);
		Thread.sleep(3000);
		ImageIO.write(buffImg, "JPEG", new File(distImgPath));
		buffImg.flush();
	}

	public static void main(String[] args) {
		String sourceFile = "D:/pic/clock-1.jpg";
		try {
			/*String tmpImg = cutJPG(sourceFile);
			resizeImage(tmpImg, sourceFile, 10);

			if (tmpImg.endsWith("Tmp.jpg")) {
				File tmp = new File(tmpImg);
				tmp.delete();
			}*/
			resizeImage(sourceFile, sourceFile, 2);
		} catch (IOException e) {
			System.out.println("图片转换出现异常！");
		} catch (InterruptedException e) {
			System.out.println("图片转换出现异常！");
		}

	}
}
